<?php
namespace cosocket\application;

use cosocket\application\managers\ConnectorManager;

/**
 * 控制器基类
 */
class Controller
{
    protected $ip;
    protected $params;

    public function __construct(& $params = array(), $clientInfo = array())
    {
        $this->ip = $clientInfo['remote_ip'];
        $this->params = & $params;
        $this->init();
    }

    protected function init()
    {
    }

    public function & get_ip()
    {
        return $this->ip;
    }

    public function & get_params()
    {
        return $this->params;
    }

    protected function response_success_to_user($uid, $c, $m, $data)
    {
        $cmd = [
            'c' => $c,
            'm' => $m,
            'code' => 0,
            'msg' => '成功',
            'data' => $data,
        ];
        ConnectorManager::send_to_user($uid, json_encode($cmd));
    }

    protected function response_error_to_user($uid, $c, $m, $code, $msg, $data)
    {
        if (empty($msg)) {
            $msg = ErrorCode::getMessage($code);
        }

        $cmd = [
            'c' => $c,
            'm' => $m,
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
        ConnectorManager::send_to_user($uid, json_encode($cmd));
    }

    protected function response_success_to_all($c, $m, $data)
    {
        $cmd = [
            'c' => $c,
            'm' => $m,
            'code' => 0,
            'msg' => '成功',
            'data' => $data,
        ];
        ConnectorManager::send_to_all(json_encode($cmd));
    }

}
